package pl.com.like.sequoia.dao.security;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;

import pl.com.like.sequoia.dao.GenericDao;
import pl.com.like.sequoia.model.security.Authorization;
import pl.com.like.sequoia.model.security.User;


public class AuthorizationDao extends GenericDao<Authorization, Long> implements IAuthorizationDao {

	public AuthorizationDao() {
		super(Authorization.class);
	}
	
	/** {@inheritDoc} */
	@SuppressWarnings("unchecked")
	public Boolean isAuthorized(User user, Class<?> clazz) {
		Boolean isAuth = Boolean.FALSE;
		if (null != user && null != clazz) {
			String canonicalName = clazz.getCanonicalName();
			Criteria crit = getCriteria().add(Expression.like("clazz", canonicalName));
			crit.add(Expression.eq("user", user));
			List<Authorization> list = crit.list();
			if ((list == null) || (list.isEmpty()) || (list.size() > 1)) {
				;
			} else {
				isAuth = Boolean.TRUE;
			}
		} 
		return isAuth;
	}

}
